// pages/search/index.js
import {
  request
} from "../../request/index.js";
import regeneratorRuntime from '../../lib/runtime/runtime';
Page({

  /**
   * 页面的初始数据
   */
  data: {
    goods: [],
    // 取消 按钮 是否显示
    isFocus: false,
    // 输入框的值
    inpValue: ""
  },
  TimeId:-1,
  whatInput:'',
  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },
  handleInput(e) {
    let value = e.detail.value
    this.whatInput = value
    if (!value.trim()) {
      this.setData({
        goods:[],
        isFocus:false 
      })
      console.log(this.data.goods);
      return
    }
    console.log(1111);
    this.setData({
      isFocus:true
    })
    //发送请求获取数据
    clearTimeout(this.TimeId);
    this.TimeId=setTimeout(()=>{
      this.qsearch(value)
    },1000)
  
  },
  //获取数据
  async qsearch(query) {
    if(!this.whatInput){
      this.setData({
        goods:[],
        isFocus:false
      })
      return
    }
    const res=await request({url:"/goods/qsearch",data:{query}});
    this.setData({
      goods:res
    })

  },
  handleCancel(){
    this.setData({
      inpValue:"",
      isFocus:false,
      goods:[]
    })
  }
})